//
//  TSMsgStatisticsTableView.m
//  chart
//
//  Created by xu on 15/8/15.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSMsgStatisticsTableView.h"
#import "TSMsgStatisticsModel.h"

#define STATISTICS_CELL_HEGITH 50.f
#define STATISTICS_BTN_WIDTH 50.f

@implementation TSMsgStatisticsTableView{
    TSMsgStatisticsModel *msgListModel;
    NSMutableArray *dataArray;
}

@synthesize msgId;

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self=[super initWithFrame:frame style:style]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hasLogined:) name:TSNotification_HasLogined object:nil];
        
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];
        
        self.dataSource = self;
        self.delegate = self;
        
        msgListModel = [[TSMsgStatisticsModel alloc] init];
    }
    
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cell_identify = @"cellIdentify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
    }
    
    if (dataArray && dataArray.count > indexPath.row) {
        TSReceiveMsgModel *model = [dataArray objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", model.receiveName, [TSTools getDateByTime:model.time]];
        [cell.textLabel setFont:FONT(12.f)];
        cell.detailTextLabel.text = [TSTools getReceiveStatusTypeString:model.receiveStatus.integerValue];
        [cell.detailTextLabel setFont:FONT(12.f)];
    }
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray) {
        return [dataArray count];
    }
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return STATISTICS_CELL_HEGITH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return STATISTICS_CELL_HEGITH;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(MARGIN_LEFT, 0, currentDeviceWidth-2*MARGIN_LEFT, STATISTICS_CELL_HEGITH)];
    [view setBackgroundColor:[UIColor lightGrayColor]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(MARGIN_LEFT, 0, CGRectGetWidth(view.frame)-STATISTICS_BTN_WIDTH-MARGIN_LEFT, STATISTICS_CELL_HEGITH)];
    [label setText:[NSString stringWithFormat:@"总共:%@人 未读:%@人, 确认:%@人, 请假:%@人, 委托:%@人", msgListModel.all, msgListModel.wait, msgListModel.affirm, msgListModel.leave, msgListModel.affirm]];
    [label setFont:FONT(12.f)];
    [view addSubview:label];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame)+MARGIN_LEFT, MARGIN_LEFT, STATISTICS_BTN_WIDTH, STATISTICS_CELL_HEGITH-MARGIN_LEFT*2)];
    btn.layer.borderWidth = 1.f;
    btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    [btn setTitle:@"统计" forState:UIControlStateNormal];
    [view addSubview:btn];
    
    return view;
}


#pragma mark - requestData

- (void)hasLogined:(NSNotification *)noti
{
    [self.header beginRefreshing];
}

- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    msgListModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize), TSDictionaryKeyDefine_Id:msgId?:@""};
    [msgListModel fetchWithPath:kRequestUrl_SendMessage_outList_message_allMessage completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (msgListModel.all.integerValue > 0) {
            strongSelf.footer.hidden = NO;
            
            [weakSelf insertAllListToDataArray];
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

- (void)footerRequestData
{
    WS(weakSelf);
    self.pageIndex++;
    msgListModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize), TSDictionaryKeyDefine_Id:msgId?:@""};
    [msgListModel fetchWithPath:kRequestUrl_SendMessage_outList_message_allMessage completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        
        if (msgListModel.all.integerValue == 0) {
            strongSelf.footer.hidden = YES;
        }
        else{
            [weakSelf insertAllListToDataArray];
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

- (void)insertAllListToDataArray
{
    if (msgListModel) {
        dataArray = [[NSMutableArray alloc] initWithCapacity:0];
        if (msgListModel.waitList && msgListModel.waitList.count > 0) {
            [dataArray addObjectsFromArray:msgListModel.waitList];
        }
        
        if (msgListModel.affirmList && msgListModel.affirmList.count > 0) {
            [dataArray addObjectsFromArray:msgListModel.affirmList];
        }
        
        if (msgListModel.leaveList && msgListModel.leaveList.count > 0) {
            [dataArray addObjectsFromArray:msgListModel.leaveList];
        }
        
        if (msgListModel.entrustList && msgListModel.entrustList.count > 0) {
            [dataArray addObjectsFromArray:msgListModel.entrustList];
        }
    }
}




@end
